import 'package:flower/utils/my_colors.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../../../../components/cached_image_widget.dart';
import '../../../../config/service_url.dart';
import '../../../../model/flower_list.dart';
import '../../../../utils/http_util.dart';
import 'package:dio/dio.dart';


class Point extends StatefulWidget {
  const Point({super.key});

  @override
  _Point createState() => _Point();
}

class _Point extends State<Point> {
  List<FlowerData> flowerList = [];

  @override
  void initState() {
    super.initState();
    queryFlowerList();
  }

  void queryFlowerList() async {
    Response result = await HttpUtil.get(getFlowerList);
    FlowerListModel flowerModel = FlowerListModel.fromJson(result.data);
    setState(() {
      flowerList = flowerModel.data!;
    });
  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: const Text(
          '我的积分',
        ),
        centerTitle: true,
      ),
      body: ListView(
        children: [
          buttonPoint(context),
          curated(),
        ],
      ),
    );
  }


  //积分兑换商品
  Widget curated() {
    return GridView.builder(
      padding: const EdgeInsets.all(10),
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 2,
        crossAxisSpacing: 10.0,
        mainAxisSpacing: 10.0,
        childAspectRatio: 0.75,
      ),
      itemCount: flowerList.length,
      itemBuilder: (context, index) {
        return GestureDetector(
          onTap: () {
            // Navigator.push(
            //   context,
            //   MaterialPageRoute(
            //     builder: (context) => FlowerDetailPonitPage(flowerId: flowerList[index].id),
            //   ),
            // );
          },
          child: Card(
            elevation: 3,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(10),
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Expanded(
                  child: ClipRRect(
                    borderRadius: const BorderRadius.vertical(top: Radius.circular(10)),
                    child: CachedImageWidget(
                      163,
                      135,
                      flowerList[index].coverUrl,
                      fit: BoxFit.cover,
                    ),
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        flowerList[index].name,
                        style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
                      ),
                      const SizedBox(height: 5),
                      Text(
                        flowerList[index].ename,
                        style: const TextStyle(
                          fontSize: 14,
                          color: MyColors.ename,
                        ),
                      ),
                      const SizedBox(height: 5),
                      Text(
                        "555积分",
                        style: const TextStyle(
                          fontSize: 14,
                          color: Colors.red,
                        ),
                      ),
                      const SizedBox(height: 5),
                    ],
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );
  }

  //积分总数
  Widget buttonPoint(BuildContext context) {
    return Padding(
      padding: EdgeInsets.all(15),
      child: Card(
          elevation: 3,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(10),
          ),
          child: Padding(
            padding: EdgeInsets.all(15),
            child: Column(
              children: [
                Row(
                  children: [
                    Text(
                      '  3',
                      style: TextStyle(
                        fontSize: 22,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    Text(
                      '   积分',
                      style: TextStyle(
                        fontSize: 14,
                      ),
                    ),
                    Column(
                      children: [
                        Container(
                          margin: EdgeInsets.only(left: 20.0),
                          child: InkWell(
                            onTap: () {
                              navigateToHome(context); // 调用方法来执行导航
                            },
                            child: Column(
                              children: [
                                Text(
                                  '积分不够？点此赚取积分',
                                  style: TextStyle(
                                    color: MyColors.boyPink,
                                    fontSize: 12,
                                    fontWeight: FontWeight.bold,
                                  ),
                                ),
                              ],
                            ),
                          ),
                        )
                      ],
                    ),
                    // ),
                  ],
                ),
                Column(
                  children: [
                    Text(
                      '    积分说明：购买商品即可获得对应的积分，满足商品要求即可兑换您想要的花',
                      style: TextStyle(
                        fontSize: 10,
                        color: MyColors.title,
                      ),
                    ),
                  ],
                ),
              ],
            ),
          )),
    );
  }

  void navigateToHome(BuildContext context) {
    Navigator.of(context).pushNamed('/index');
  }
}
